/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "bsp.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */
static TaskHandle_t xHandleTaskStart = NULL;
static TaskHandle_t xHandleTaskUartShell = NULL;

SemaphoreHandle_t xTxSemaphore;  // 用于发送完成的信号量
SemaphoreHandle_t xUartMutex;    // 用于保护串口资源的互斥锁
/* USER CODE END Variables */
/* Definitions for defaultTask */
osThreadId_t defaultTaskHandle;
const osThreadAttr_t defaultTask_attributes = {
  .name = "defaultTask",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */
static void AppTaskStart(void *argument);
static void AppTaskCreate(void);
static void AppTaskUartShell(void *argument);
/* USER CODE END FunctionPrototypes */

void StartDefaultTask(void *argument);

void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
	xUartMutex = xSemaphoreCreateMutex();

  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
	xTxSemaphore = xSemaphoreCreateBinary();
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* creation of defaultTask */
  defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
	xTaskCreate(  AppTaskStart,         /* ????  */
								"AppTaskStart",       /* ???    */
								512,                 	/* ?????,??word,???4?? */
								NULL,                 /* ????  */
								osPriorityNormal6,    /* ?????*/
								&xHandleTaskStart );  /* ????  */
  /* USER CODE END RTOS_THREADS */

  /* USER CODE BEGIN RTOS_EVENTS */
  /* add events, ... */
  /* USER CODE END RTOS_EVENTS */

}

/* USER CODE BEGIN Header_StartDefaultTask */
/**
  * @brief  Function implementing the defaultTask thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_StartDefaultTask */
void StartDefaultTask(void *argument)
{
  /* USER CODE BEGIN StartDefaultTask */
  /* Infinite loop */
  for(;;)
  {
    osDelay(1);
  }
  /* USER CODE END StartDefaultTask */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */
/*
*********************************************************************************************************
*	函 数 名: AppTaskStart
*	功能说明: 启动任务，这里用作BSP驱动包处理。
*	形    参: 无
*	返 回 值: 无
* 优 先 级: osPriorityNormal6  
*********************************************************************************************************
*/
static void AppTaskStart(void *argument)
{
	uint8_t  ucCount = 0;

	AppTaskCreate();
	
	while(1)
	{
		/* 需要周期性处理的程序 */
		
		
		/* 1ms 处理 */
		bsp_RunPer1ms();
		/* 10ms 处理 */
		if(++ucCount == 10)
		{
			ucCount = 0;
			bsp_RunPer10ms();
		}
		vTaskDelay(1);
	}
}

/*
*********************************************************************************************************
*	函 数 名: AppTaskCreate
*	功能说明: 创建应用任务
*	形    参：无
*	返 回 值: 无
*********************************************************************************************************
*/
static void AppTaskCreate(void)
{
	BaseType_t status;


	
	status = xTaskCreate(  AppTaskUartShell,  			 /* 任务函数  */
												"AppTaskUartShell", 			 /* 任务名    */
												512,                 			 /* 任务栈大小，单位word，也就是4字节 */
												NULL,                 		 /* 任务参数  */
												osPriorityNormal5,    		 /* 任务优先级*/
												&xHandleTaskUartShell );   /* 任务句柄  */
	if ( pdPASS == status)
		shellPrint(&shell, "[AppTaskCreate]:AppTaskUartShell task create success\r\n");
	else
		shellPrint(&shell, "[AppTaskCreate]:AppTaskUartShell task create failed\r\n");
	
	shellPrint(&shell, "*************************************************************\r\n");
}


/*
*********************************************************************************************************
*	函 数 名: AppTaskUartShell
*	功能说明: 串口shell任务，用于接收串口数据，并执行相关命令。
*	形    参: 无
*	返 回 值: 无
* 优 先 级: osPriorityNormal5
*********************************************************************************************************
*/
static void AppTaskUartShell(void *argument)
{
	/* 初始化任务 */
	
	while(1)
	{
		/* 需要周期性处理的程序 */
		shellTask(&shell);
		vTaskDelay(1);
	}
}
/*
*********************************************************************************************************
*	函 数 名: PrintfLogo
*	功能说明: 打印例程名称和例程发布日期, 接上串口线后，打开PC机的超级终端软件可以观察结果
*	形    参: 无
*	返 回 值: 无
*********************************************************************************************************
*/
void PrintfLogo(void)
{
	
	/* 检测CPU ID */
	uint32_t CPU_Sn0, CPU_Sn1, CPU_Sn2;
	
	CPU_Sn0 = HAL_GetUIDw0();
	CPU_Sn1 = HAL_GetUIDw1();
	CPU_Sn2 = HAL_GetUIDw2();
	
	shellPrint(&shell, "*************************************************************\r\n");
	shellPrint(&shell, "    __________  ______   ______________ \r\n");
	shellPrint(&shell, "   / ____/ __ \\/ ____/  / ____/_  __/ / \r\n");
	shellPrint(&shell, "  / /_  / / / / /      / /     / / / /  \r\n");
	shellPrint(&shell, " / __/ / /_/ / /___   / /___  / / / /___\r\n");
	shellPrint(&shell, "/_/    \\____/\\____/   \\____/ /_/ /_____/\r\n");
	shellPrint(&shell, "                                        \r\n");
	shellPrint(&shell, "Build:       "__DATE__" "__TIME__"\r\n");
	shellPrint(&shell, "Version:     "SOFTWARE_VERSION"\r\n");
	shellPrint(&shell, "Copyright:   (C) 2025 astronG\r\n");
	shellPrint(&shell, "-------------------------------------------------------------\r\n");
	shellPrint(&shell, "CPU: STM32G474RET6, SYSCLK: %dMHz, RAM: 128KB, ROM: 512KB\r\n", SystemCoreClock / 1000000);
	shellPrint(&shell, "UID: %08X %08X %08X\r\n", CPU_Sn2, CPU_Sn1, CPU_Sn0);
	shellPrint(&shell, "STM32G4xx_HAL_Driver:STM32Cube_FW_G4 V1.6.1\r\n");
//	shellPrint(&shell, "Current version of FOC Algorithm: %s\r\n", MYFOC_SOFTWARE_VERSION);
  shellPrint(&shell, "*************************************************************\r\n");
}

/* USER CODE END Application */

